﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NumberTest.Question;

namespace NumberTest.Answer
{
    class Albin: iNumberTest
    {
        public long[] arr = new long[10] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
        int flag = 0;
        long start1 = 0;
        long end1 = 0;
        long start3 = 0;
        long end3 = 0;
        long pex = 1;
        public int CountTest(long start, long end)
        {
            string start2 = start.ToString();
            string end2 = end.ToString();
            int n = 0;
            long s = 0;
            if (start == end)
            {
                chai(start);
            }
            else
            {
                per( start, end);
                string pex2 = pex.ToString();
                if (start2.Length < end2.Length)
                {
                    while (end / pex != 0)
                    {
                        arr[end / pex % 10] += end % pex + 1;
                        for (int i = 1; i < end / pex % 10; i++)
                        {
                            arr[i] += pex;
                        }
                        if (end/pex/10 != 0)
                        {
                            arr[0] -= pex;
                        }
                        if (long.MaxValue / 10 > pex)
                        {
                            pex *= 10;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i <= 9; i++)
            {
                if (arr[i] > s)
                {
                    s = arr[i];
                    n = i;
                }
            }
            return n;
        }
        public void per( long start, long end)
        {

            string start2 = start.ToString();
            string end2 = end.ToString();
            long start4 = start;
            long end4 = end;
            int flag2 = 0;
            do
            {
                if (start2.Length == 1)
                {
                    flag2 = 1;
                }
                start1 = start % 10;
                end1 = end % 10;
                if (start1 < end1 + 1)
                {
                    flag = 0;
                    if (pex > 1)
                    {
                        arr[start1] += pex - start4 % pex;
                        arr[end1] += end4 % pex + 1;
                    }
                    else
                    {
                        arr[start1]++;
                        arr[end1]++;
                    }
                    for (long i = start1+1; i < end1; i++)
                    {
                        arr[i] += pex;
                       
                    }
                }
                else 
                {
                    flag = 1;
                    arr[start1] += pex - start4 % pex;
                    arr[end1] += end4 % pex + 1;
                    for (long i = 0; i < end1; i++)
                    {
                        arr[i] += pex;
                    }
                    for (long i = start1 + 1; i <= 9; i++)
                    {
                        arr[i] += pex;
                    }
                   
                }
                pex *= 10;
                start3 = start % pex;
                end3 = end % pex;
                start = start / 10;
                end = end / 10;
                start2 = start.ToString();
                end2 = end.ToString();
               
            }
            while (flag2 == 0);
            start = start4;
            end = end4;
        }
        public void chai(long start)
        {
            int len = start.ToString().Length;
            while (len-- > 0)
            {
                arr[start % 10]++;
                start = start / 10;
            } 
        }
      
    }
  
}
